package com.ouyeel.wlxer.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.adapter.MenuItemAdapter;
import com.ouyeel.wlxer.base.BaseActivity;
import com.ouyeel.wlxer.bean.MenuItem;
import com.ouyeel.wlxer.util.MenuItemFactory;
import com.ouyeel.wlxer.util.SharePrefUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

import static com.ouyeel.wlxer.util.MenuItemFactory.TAG_OFFER;
import static com.ouyeel.wlxer.util.MenuItemFactory.TAG_RECORD;
import static com.ouyeel.wlxer.util.MenuItemFactory.TAG_TASK;


public class MainActivity extends BaseActivity {
    @BindView(R.id.grid_view)
    GridView gridView;
    private static final String TAG = MainActivity.class.getSimpleName();
    boolean exit = false;
    private List<MenuItem> menuItemList;


    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        showTitleAndLeftImg("首页");
        if (menuItemList == null) {
            menuItemList = new ArrayList<>();
        }
        menuItemList = MenuItemFactory.getInstanse().setMenuItems();
        MenuItemAdapter menuItemAdapter = new MenuItemAdapter(this, menuItemList);
        gridView.setAdapter(menuItemAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                int tag = menuItemList.get(position).getTag();
                switch (tag) {
                    case TAG_TASK:
                        showTost("我是任务");
                        break;
                    case TAG_OFFER:
                        startActivity(new Intent(MainActivity.this, QuoteActivity.class));
                        break;
                    case TAG_RECORD:
                        showTost("我是记录");
                        break;
                }
            }
        });
    }


    @Override
    public void onBackPressed() {
        if (!exit) {
            exit = true;
            showTost("再按一次退出物流小二");
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    exit = false;
                }
            }, 2000);
        } else {
            SharePrefUtil.clear(this);
            finish();
            System.exit(0);
        }
    }

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.finish();
    }

    // TODO: 2017/11/15 左边图片点击事件 
    @Override
    public void leftImageOnclick() {
        super.leftImageOnclick();
        // TODO: 2017/11/15 跳转到个人中心
        Intent mIntent = new Intent(MainActivity.this, PersonalCenterActivity.class);
        startActivity(mIntent);
    }
}
